const path=require('path');
const os = require('os');
const webpack=require('webpack');
const HtmlWebpackPlugin=require('html-webpack-plugin');
const ExtractTextPlugin=require('extract-text-webpack-plugin');
var HappyPack = require('happypack');
const autoprefixer = require('autoprefixer');
var happyThreadPool = HappyPack.ThreadPool({size: os.cpus().length});
const {environment,project, version} = require('../build/project');
function resolve(dir) {
    return path.join(__dirname, '..', dir);
}
const isProd=environment==='production';
let webpackConfig = {
    entry: {
        index: [
            //主入口
            './src/projects/' + project + '/main.ts'
        ]
    },
    resolve: {
        extensions: ['.ts',  '.js', '.jsx', '.json', '.less', '.css'],
        modules: [
            resolve('../node_modules'),
            resolve('../src')
        ],
        alias: {
            '@': resolve('../src'),
            project: resolve('../webpack/build/project'),
            assets: resolve('../src/assets'),
        }
    },
    module:{
        rules:[
            {
                test:/\.ts$/,
                use:isProd?['@ngtools/webpack']:['ts-loader', 'angular-router-loader', 'angular2-template-loader']
            },{
                test: /\.html?$/,
                loader: 'raw-loader',
                include: [resolve('../src')],
                exclude: [/node_modules/]
            },
            {
                test: /\.css/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader'
                }),

            },
            {
                test: /\.less$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            minimize: true,
                            importLoaders: 1,
                            modules: true,
                            localIdentName: '[name]__[local]-[hash:base64:5]',
                            localIndexName: '[name]__[local]___[hash:base64:5]',
                        }
                    }, {
                        loader: require.resolve('postcss-loader'),
                        options: {
                            ident: 'postcss',
                            plugins: () => [
                                require('postcss-flexbugs-fixes'),
                                autoprefixer({
                                    browsers: [
                                        'last 10 versions',
                                        'ie >= 6',
                                        'ie_mob >= 6',
                                        'ff >= 10',
                                        'chrome >= 14',
                                        'safari >= 3',
                                        'ios >= 4',
                                        'android >= 4.0'
                                    ]
                                })
                            ],
                        },
                    },
                    {
                        loader: require.resolve('less-loader'), // compiles Less to CSS
                    }
                ]


            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: [/node_modules/],
                include: [resolve('../src')]
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                query: {
                    limit: 10000,
                    name: path.posix.join('static', 'img/[name].[ext]'),
                },
                exclude: [/node_modules/],
                include: [resolve('../src')]
            },
            {
                test: /\.(woff|woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader?limit=1000000',
                query: {
                    limit: 1000,
                    name: path.posix.join('static', 'fonts/[name].[ext]')
                },
                include: [resolve('../src'), resolve('../node_modules/element-ui')]
            },
            {
                test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 1000,
                    name: path.posix.join('static', 'media/[name].[hash:7].[ext]')
                },
                exclude: [/node_modules/],
                include: [resolve('../src')]
            }]
    },
    plugins: [

        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: 'src/projects/' + project + '/index.html',
            inject: true,
            version: version
        }),
        new webpack.LoaderOptionsPlugin({
            debug:!isProd,
            minimize:isProd
        }),
        new webpack.optimize.CommonsChunkPlugin({
            names:["vendor"],
            "minChunks":2,
            "async":"common"
        }),
        new ExtractTextPlugin('./css/[name].css?ver=[hash]'),
        new HappyPack({
            id: 'happybabel',
            loaders: ['babel-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new HappyPack({
            id: 'happyurl',
            loaders: ['url-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new HappyPack({
            id: 'happystyle',
            loaders: ['style-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new HappyPack({
            id: 'happypostcss',
            loaders: ['postcss-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new HappyPack({
            id: 'happyless',
            loaders: ['less-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new HappyPack({
            id: 'happycss',
            loaders: ['css-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        })
    ]
};

module.exports = webpackConfig;
